$(function() {
	window.onload = function() {
		addPic(fileElem, fileList);
		addPic(fileElem2, fileList2);
		addPic(fileElem3, fileList3);
		addPic(fileElem4, fileList4);
		addPic(fileElem5, fileList5);
	}

	window.URL = window.URL || window.webkitURL;
	var fileElem = getId('file_input'),
		fileList = getId("fileList"),
		fileElem2 = getId('file_input2'),
		fileList2 = getId("fileList2"),
		fileElem3 = getId('file_input3'),
		fileList3 = getId("fileList3"),
		fileElem4 = getId('file_input4'),
		fileList4 = getId("fileList4"),
		fileElem5 = getId('file_input5'),
		fileList5 = getId("fileList5");

	function handleFiles(obj, target) {
		var files = obj.files,
			img = new Image();
		if(window.URL) {
			img.src = window.URL.createObjectURL(files[0]); //创建一个object URL，并不是你的本地路径
			img.width = 200;
			img.height = 140;
			img.onload = function(e) {
				window.URL.revokeObjectURL(this.src); //图片加载后，释放object URL
			}
			target.appendChild(img);
		} else if(window.FileReader) {
			//opera不支持createObjectURL/revokeObjectURL方法。我们用FileReader对象来处理
			var reader = new FileReader();
			reader.readAsDataURL(files[0]);
			reader.onload = function(e) {
				alert(files[0].name + "," + e.total + " bytes");
				img.src = this.result;
				img.width = 200;
				img.height = 140;
				target.appendChild(img);
			}
		} else {
			//ie
			obj.select();
			obj.blur();
			var nfile = document.selection.createRange().text;
			document.selection.empty();
			img.src = nfile;
			img.width = 200;
			img.onload = function() {
				alert(nfile + "," + img.fileSize + " bytes");
			}
			target.appendChild(img);
		}
	}

	function getId(str) {
		return document.getElementById(str);
	}

	function addPic(fileE, fileL) {
		fileE.onchange = function() {
			if(fileL.childNodes != 0) {
				var childs = fileL.childNodes;
				for(var i = 0; i < childs.length; i++) {
					fileL5.removeChild(childs[i]);
				}
			};
			handleFiles(fileE, fileL);
		}
	}

	$(".submit_final").click(function() {
		var token = localStorage.phone;
		var file_input = $("#file_input").val();
		var file_input2 = $("#file_input2").val();
		var file_input3 = $("#file_input3").val();
		var file_input4 = $("#file_input4").val();
		var file_input5 = $("#file_input5").val();
		if(file_input == '' || file_input2 == '' || file_input3 == '' || file_input4 == '' || file_input5 == '') {
			layer.msg("请填写完整信息");
			return false;
		}
		var message = $("#message").val();
		$.ajax({
			url: 'https://newapp.icandrive.cn/api/exam_site.php',
			dataType: 'json',
			type: 'post',
			data: {
				token: token,
				message: message
			},
			success: function(data) {
				if(data.status == 250) {
					layer.msg("请重新登录");
					setInterval(function() {
						window.location.href = "javascript:history.back()";
					}, 3000);
				}
				if(data.status == 200) {
					layer.msg("提交成功");
					setInterval(function() {
						window.location.href = "javascript:history.back()";
					}, 3000);
				}
				if(data.status == 450) {
					layer.msg("不能重复提交");
					return false;
				}
				if(data.status == 400) {
					layer.msg("提交失败");
				}
			},
			error: function() {
				layer.msg('系统繁忙', function() {
					history.go(0)
				}, 1000);
			}
		});
	});

	$('#file_input').click();
	$('#file_input').change(function() {
		UpladFile();
	})

	var xhr;
	var token = localStorage.phone;

	function createXMLHttpRequest() {
		if(window.ActiveXObject) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		} else if(window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		}
	}

	function UpladFile() {

		var fileObj = document.getElementById("file_input").files[0];
		var FileController = 'https://newapp.icandrive.cn/api/ID_img.php?token=' + token;
		var form = new FormData();
		form.append("myfile", fileObj);
		createXMLHttpRequest();
		xhr.onreadystatechange = handleStateChange;
		xhr.open("post", FileController, true);
		xhr.send(form);
	}

	function handleStateChange() {
		if(xhr.readyState == 4) {
			if(xhr.status == 200 || xhr.status == 0) {
				var result = xhr.responseText;
				var json = eval("(" + result + ")");
			}
		}
	}

	//  上传user_img
	$('#file_input2').click();
	$('#file_input2').change(function() {
		UpladFile1();
	})

	function UpladFile1() {

		var fileObj = document.getElementById("file_input2").files[0];
		var FileController = 'https://newapp.icandrive.cn/api/user_img.php?token=' + token;
		var form = new FormData();
		form.append("myfile", fileObj);
		createXMLHttpRequest();
		xhr.onreadystatechange = handleStateChange;
		xhr.open("post", FileController, true);
		xhr.send(form);
	}

	$('#file_input3').click();
	$('#file_input3').change(function() {
		UpladFile2();
	})

	function UpladFile2() {
		var fileObj = document.getElementById("file_input3").files[0];
		var FileController = 'https://newapp.icandrive.cn/api/live_img.php?token=' + token;
		var form = new FormData();
		form.append("myfile", fileObj);
		createXMLHttpRequest();
		xhr.onreadystatechange = handleStateChange;
		xhr.open("post", FileController, true);
		xhr.send(form);
	}
	//check_img上传
	$('#file_input4').click();
	$('#file_input4').change(function() {
		UpladFile3();
	})

	function UpladFile3() {
		var fileObj = document.getElementById("file_input4").files[0];
		var FileController = 'https://newapp.icandrive.cn/api/check_img.php?token=' + token;
		var form = new FormData();
		form.append("myfile", fileObj);
		createXMLHttpRequest();
		xhr.onreadystatechange = handleStateChange;
		xhr.open("post", FileController, true);
		xhr.send(form);
	}
	//receipt_img上传
	$('#file_input5').click();
	$('#file_input5').change(function() {
		UpladFile4();
	})

	function UpladFile4() {
		var fileObj = document.getElementById("file_input5").files[0];
		var FileController = 'https://newapp.icandrive.cn/api/receipt_img.php?token=' + token;
		var form = new FormData();
		form.append("myfile", fileObj);
		createXMLHttpRequest();
		xhr.onreadystatechange = handleStateChange;
		xhr.open("post", FileController, true);
		xhr.send(form);
	}

}) /*end $function*/

/*监听手机返回按键*/
return_keybord();
